package inv.stask.ui;

import inv.stask.beans.Task;
import inv.stask.logic.TaskUtil;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.AbstractCellEditor;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

public class DateCellEditor extends AbstractCellEditor implements
		TableCellEditor, ActionListener {
	
	private static final long serialVersionUID = 7343277733585970728L;
	DateCellEditorPanel panel;
	protected static final String EDIT = "edit";
	JLabel currentDate;

	
	public DateCellEditor(DatePickerAction callback) {
		panel	= new DateCellEditorPanel(callback);
		panel.setBackground(Color.BLUE);
	}

	public Component getTableCellEditorComponent(JTable table, Object value,
			boolean isSelected, int row, int column) {
		panel.setTask((Task)value);
		return panel;
	}

	public Object getCellEditorValue() {
		return currentDate;
	}

	public void actionPerformed(ActionEvent e) {
		System.out.println("deeeeee");
	}
	
	public static void updateButtonOnWeekend(JComponent label, Date date){
		Calendar cal	= Calendar.getInstance();
		cal.setTime(date);
		if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
			label.setBorder(BorderFactory.createLineBorder(Color.decode("0xD2691E"), 2));
		else
			label.setBorder(null);
	}
	
	public static void updateButtonOnDeadline(JComponent label, Date date, boolean done){
		Calendar cal	= Calendar.getInstance();
		cal.setTime(date);
		Date currentDate	= new Date();
		SimpleDateFormat sdf	= new SimpleDateFormat("yyyyMMdd");
		
		String today	= sdf.format(currentDate);
		String sDate	= sdf.format(date);
		if(!done && today.compareTo(sDate) > 0)
			label.setForeground(Color.RED);
		else
			label.setForeground(null);
	}
	
	private static Icon[] postponedIcons = new Icon[]{
		new ImageIcon(DateCellRenderer.class.getResource("/inv/stask/icons/bullet_pink.png")),
		new ImageIcon(DateCellRenderer.class.getResource("/inv/stask/icons/bullet_orange.png")),
		new ImageIcon(DateCellRenderer.class.getResource("/inv/stask/icons/bullet_red.png")),
	};
	
	public static void updateButtonPostoned(JComponent label, int postponed){
		JLabel jLabel = ((JLabel)label);
		int percent	= postponed / 3;
		
		String tooltip	= "";
		if(percent == 0)
			jLabel.setIcon(null);
		else{
			if(percent == 1)
				jLabel.setIcon(postponedIcons[0]);
			else
				if(percent == 2)
					jLabel.setIcon(postponedIcons[1]);
				else
					if(percent >= 3)
						jLabel.setIcon(postponedIcons[2]);
			tooltip	= "postponed "+postponed+" times";
		}
		
		jLabel.setToolTipText(tooltip);
	}

}

class DateCellEditorPanel extends JPanel{
	private static final long serialVersionUID = 3806013610507492033L;
	
	final JButton buttonPlus	= new JSimpleButton("+");
	final JButton label			= new JSimpleTextButton("..");
	final JButton buttonMinus	= new JSimpleButton("-");
	private Task task;
	
	{
		buttonPlus.setBackground(Color.decode("0x778899"));
		buttonMinus.setBackground(Color.decode("0x778899"));
		
		buttonPlus.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				Calendar cal	= Calendar.getInstance();
				cal.setTime(task.getDueDate());
				
				cal.add(Calendar.DAY_OF_MONTH, 1);
				task.setDueDate(cal.getTime());
				updateLabel();
			}
		});
		
		buttonMinus.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				Calendar cal	= Calendar.getInstance();
				cal.setTime(task.getDueDate());
				
				cal.add(Calendar.DAY_OF_MONTH, -1);
				task.setDueDate(cal.getTime());
				updateLabel();
			}
		});
		
	}
	
	public DateCellEditorPanel(final DatePickerAction callback) {
		super();
		
		
		GridLayout layout	= new GridLayout(1, 3);

		setLayout(layout);
		initButtons();
		
		label.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				Date date = callback.showDatePicker(task.getDueDate());
				if(date != null && date.compareTo(task.getDueDate()) != 0){
					getTask().setDueDate(date);
					callback.showDatePickerResults();
				}
			}
		});
	}
	
	private void initButtons(){
		add(buttonMinus);
		add(label);
		add(buttonPlus);
	}

	public Task getTask() {
		return task;
	}

	public void setTask(Task task) {
		this.task = task;
		updateLabel();
	}
	
	public void updateLabel(){
		label.setText( TaskUtil.dayFormat.format(task.getDueDate()) );
		DateCellEditor.updateButtonOnWeekend(label, task.getDueDate());
	}
	

}

class JSimpleButton extends JButton{
	private static final long serialVersionUID = 7497069569613611219L;

	public JSimpleButton(String text){
		super(text);
		Insets ins	= getMargin();
		ins.set(0, 2, 0, 2);
		setMargin(ins);
	}
}

class JSimpleTextButton extends JButton{
	private static final long serialVersionUID = 4643680698255727549L;

	public JSimpleTextButton(String text){
		super(text);
		Insets ins	= getMargin();
		ins.set(0, 4, 0, 4);
		setMargin(ins);
	}
}